package org.moflon.moca;

import java.io.IOException;

import org.moflon.moca.dat.unparser.DatUnparserAdapter;
import org.moflon.tie.TGGMain;
import org.moflon.util.eMoflonEMFUtil;

import FolksonomyLanguage.Folksonomy;
import FolksonomyLanguage.FolksonomyLanguageFactory;
import Moca.CodeAdapter;
import Moca.MocaFactory;

public class GenerateModels {
	public static void main(String[] args) throws IOException {
		
		Folksonomy folksonomy0 = createFolksonomyWithUsersAndFriends();
		transformToText(folksonomy0);

		Folksonomy folksonomy1 = createFolksonomyWithActivityStructure();
		transformToText(folksonomy1);
	}

	private static void transformToText(Folksonomy folksonomy)
			throws IOException {
		String modelPath = "instances/" + folksonomy.getName() + ".xmi";
		
		// Save model
		eMoflonEMFUtil.saveModel(folksonomy, modelPath);
		
		// Model-to-tree
		TGGMain helper = new TGGMain();
		helper.performForward(modelPath);
		
		// Tree-to-text
		CodeAdapter codeAdapter = MocaFactory.eINSTANCE.createCodeAdapter();
	    codeAdapter.getUnparser().add(new DatUnparserAdapter());
	    codeAdapter.unparse("instances/", (MocaTree.Folder) helper.getTrg());
	}

	private static Folksonomy createFolksonomyWithUsersAndFriends() {
		Folksonomy folksonomy = FolksonomyLanguageFactory.eINSTANCE.createFolksonomy();
		folksonomy.setName("UnitedWeStand");
		
		// Create 20 users
		for (int i = 0; i < 200; i++) {
			folksonomy.createUser("Jane Doe " + i);			
		}
		
		// Add 5 friendships for 3 of the users
		for (int i = 0; i < 3; i++) {	
			for(int j = 0; j < 5; j++)
				folksonomy.makeNewFriendForUser(folksonomy.getUsers().get(i)); 
		}
		
		return folksonomy;
	}
	
	private static Folksonomy createFolksonomyWithActivityStructure(){
		Folksonomy folksonomy = FolksonomyLanguageFactory.eINSTANCE.createFolksonomy();
		folksonomy.setName("EveryoneGetsAnActivity");
		
		// Create some activities
		folksonomy.createActivity("one").createSubActivity("two").createSubActivity("three");
		folksonomy.createActivity("yesterday").createSubActivity("today").createSubActivity("tomorrow").createSubActivity("someday");
		
		// Create some users
		for (int i = 0; i < 10; i++) {
			folksonomy.createUser("Max Musterman " + i);
		}
		
		// Everyone gets an activity
		folksonomy.assignActivitiesToAllUsers();
		
		return folksonomy;
	}
}
